package com.sinoma.G2project.MoYu.app;


import com.alibaba.fastjson.TypeReference;
import com.firenay.common.constant.AuthServerConstant;
import com.firenay.common.utils.R;
import com.firenay.common.vo.MemberRsepVo;
import com.sinoma.G2project.MoYu.Service.MemberService;
import com.sinoma.G2project.MoYu.exception.RegisterEnum;
import com.sinoma.G2project.MoYu.vo.UserRegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
@Slf4j
public class RegisterController {

    @Autowired
    MemberService memberService;

    @PostMapping("/register")
    public String register(UserRegisterVo userRegisterVo, RedirectAttributes redirectAttributes, HttpSession session) {
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute == null){
            return "login";
        }
        MemberRsepVo attribute1 = (MemberRsepVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
        if ("admin".equals(attribute1.getUsername())) {
            String s = memberService.register(userRegisterVo);
            HashMap<String, String> error = new HashMap<>();
            // 获取错误信息
            error.put("msg", s);
            redirectAttributes.addFlashAttribute("errors",error);

        }
        return "redirect:/register.html";
    }
}
